<?php
namespace app\index\controller;

use think\Db;
use think\Controller;
use think\Request;
use think\Session;
class User extends Controller
{
//    加载个人首页
    public function index()
    {
//        根据session判断身份
        if(Session::get('usercate')==1){
            $data = input('session.tel');
            $tel = substr($data,'0','11');
//            查询出用户主表基本信息
            $list = Db::name('user')
                    ->field('user_id,user_email,user_tel')
                    ->where('user_tel='.$tel)
                    ->find();
            $id = $list['user_id'];
//            查询用户子表相关个人信息
            $data = Db::name('usermore_information')
                    ->field('height,weight,health,brithday,school,education,admissiontime,jobintension,jobcity,jobexperience,selfdescribe,sex,username')
                    ->where('uid='.$id)->find();

            $this->assign('list',$list);
            $this->assign('data',$data);
            return view('user/userinfo');
        }else{
            echo "<script> alert('您不是个人用户');parent.location.href='http://www.zzz.com/index/login/index'; </script>";
        }
    }

    //    加载编辑页面
    public function edit()
    {
//        获取用户id
        $data = input('session.tel');
        $tel = substr($data,'0','11');
        $list = Db::name('user')->field('user_id,user_email,user_tel')->where('user_tel='.$tel)->find();
        $id = $list['user_id'];
//        查询用户信息
        $data = Db::name('usermore_information')
            ->field('height,weight,health,brithday,school,education,admissiontime,jobintension,jobcity,jobexperience,selfdescribe,sex,username')
            ->where('uid='.$id)->find();
//        查询职位与城市
        $cat = Db::name('category')->field('name,id')->where('pid= 2')->select();
        $cit = Db::name('category')->field('name,id')->where('pid= 3')->select();

        $this->assign('cat',$cat);
        $this->assign('cit',$cit);
        $this->assign('list',$list);
        $this->assign('data',$data);

        return view('user/edit');

    }

//    执行编辑页面
    public function doedit($id)
    {
//        获取post传输值并处理
        $user_email = $_POST['user_email'];
        unset($_POST['user_email']);
        $arr = ['user_email'=>$user_email];
//        执行修改
        $a = Db::name('usermore_information')->where('uid='.$id)->update($_POST);
        $b = Db::name('user')->where('user_id='.$id)->update($arr);
//        判断修改是否成功
        if ($a == 0 && $b == 0){
            echo "<script> alert('修改失败');parent.location.href=document.referrer; </script>";
        }else{
            echo "<script> alert('修改失败');parent.location.href='http://www.zzz.com/index/user/index;'; </script>";

        }
    }

    //    加载修改密码页面
    public function pass()
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];

        $this->assign('uid',$id);
        return view('user/pass');
    }

    //    执行密码修改
    public function dopass()
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        处理ajax传输数据并验证
        $newpwd = $_POST['password'];
        $renewpwd = $_POST['password1'];
        if ($newpwd !== $renewpwd) {
            $this->error('两次密码不一致');
        }
        unset($_POST['password1']);
//        密码进行哈希加密
        $newpwd = password_hash($newpwd,PASSWORD_DEFAULT);
        $upd = Db::name('user')
            ->field('user_pwd')
            ->where('user_id='.$id)
            ->find();
//        对比密码是否一致
        if (password_verify($_POST['oldpassword'], $upd['user_pwd'])) {
            Db::name('user')->where('user_id='.$id)->update(['user_pwd'=>$newpwd]);
            echo "<script> alert('修改成功');parent.location.href=document.referrer; </script>";
        } else {
            echo "<script> alert('密码错误');parent.location.href=document.referrer; </script>";
        }

    }

//    加载收藏页面
    public function mycollect()
    {
//        获取user id
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        查出user的收藏
        $upid = Db::name('user_position')->field('pid')->where('uid='.$id)->select();
//        统计收藏个数
        $num[] = count($upid);
        $ucid = Db::name('user_company')->where('uid='.$id)->where('status = 1 or 3')->count();
        $num[] = $ucid;
//        循环查询收藏职位信息
        $position = array();
        foreach ($upid as $k => $v) {
            $position[] = Db::name('position')
                ->field('position_id,position_name,position_companyid,position_place,position_start,position_end')
                ->where('position_id='.$v['pid'])
                ->find();
        }

        $this->assign('num',$num);
        $this->assign('arr',$position);
        return view('user/mycollect');
    }

//    我的兼职
    public function myjob($status)
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        获取兼职收藏时间
        $time = Db::name('apply')
            ->field('addtime')
            ->where('uid='.$id)
            ->where('status='.$status)
            ->order('id desc')
            ->select();
//        根据收藏表查职位id
        $msg = Db::name('apply')
            ->field('pid')
            ->where('uid='.$id)
            ->where('status='.$status)
            ->order('id desc')
            ->select();
//        统计不同状态的收藏总数
        $num = array();
        $num[] = Db::name('apply')->field('id')->where('uid='.$id)->where('status=1')->count();
        $num[] = Db::name('apply')->field('id')->where('uid='.$id)->where('status=2')->count();
        $num[] = Db::name('apply')->field('id')->where('uid='.$id)->where('status=3')->count();
//        审核不通过原因
        $reason = Db::name('apply')
                    ->field('reply')
                    ->where('uid='.$id)
                    ->where('status = 2')
                    ->order('id desc')
                    ->select();

        $position = array();
        $companyid = array();
        $companyname = array();
//        循环查询职位信息
        foreach ($msg as $k => $v) {
            $position[] = Db::name('position')
                ->field('position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
                ->where('position_id='.$v['pid'])
                ->find();
        }
//        循环获取公司id
        foreach ($position as $k => $v){
            $companyid[] = $v['position_companyid'];
        }
//        根据公司id获取公司名称
        foreach ($companyid as $k => $v){
            $companyname[] = Db::name('company_information')->field('company_name')->where('company_id='.$v)->find();
        }
//        两个数组信息进行合并
        $arr =array();
        foreach($companyname as $k=>$r){
            $arr[] = array_merge($r,$position[$k]);
        }
//        两个数组信息进行合并
        $a = array();
        foreach($arr as $k => $r){
            $a[] = array_merge($r,$time[$k]);
        }
//        判断是否需要未通过信息
        if ($status == 2){
            $b = array();
            foreach($a as $k => $r){
                $b[] = array_merge($r,$reason[$k]);
            }
            $this->assign('arr',$b);
        }else{
            $this->assign('arr',$a);
        }
        $this->assign('reason',$reason);
        $this->assign('status',$status);
        $this->assign('num',$num);
        return view('user/myjob');
    }

//    加载积分页面
    public function integral()
    {
        return view('user/vipjifen');
    }
//    加载积分详情
    public function jifen()
    {
        return view('user/jifen');
    }

//    企业收藏
    public function collectc()
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        根据用户id获取用户收藏企业的企业id
        $data = Db::name('user_company')
                    ->field('cid')
                    ->where('uid='.$id)
                    ->where('status=1 or 3')
                    ->select();
//        循环查询收藏企业的公司信息
        $arr = array();
        foreach($data as $k=>$v){
            $arr[] = Db::name('company_information')
                        ->field('company_name,company_address,company_intro,company_cid')
                        ->where('company_cid='.$v['cid'])
                        ->find();
        }
//        统计用户收藏企业和职位的个数
        $num[] = Db::name('user_company')->where('uid='.$id)->where('status = 1 or 3')->count();
        $num[] = Db::name('user_position')->field('pid')->where('uid='.$id)->count();
        $this->assign('num',$num);
        $this->assign('arr',$arr);
        return view('user/ccompany');
    }

//    加载收藏企业的详情
    public function Companydetails($cid)
    {
//        查询当前企业的详细信息
        $data =  Db::name('company_information')
            ->field('company_name,company_address,company_intro,company_cid,company_status')
            ->where('company_cid='.$cid)
            ->find();
//        查询该企业的其他发布职位
        $arr = Db::name('position')
                        ->field('position_payway,position_name,position_id')
                        ->where('position_companyid='.$cid)
                        ->select();
        $this->assign('arr',$arr);
        $this->assign('data',$data);
        return view('user/comdetails');
    }

//    移除企业收藏
    public function collectdel($company_id)
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        字符串转换为数组
        $arr1 = explode(',',$company_id);
//        循环删除企业收藏数据
        foreach($arr1 as $k){
            Db::name('user_company')->where('uid='.$id)->where('cid='.$k)->delete();
        }
        $this->redirect('index/user/collectc');
    }

//    移除职位收藏
    public function collectjobdel($position_id)
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        字符串转换为数组
        $arr1 = explode(',',$position_id);
//        循环删除企业收藏数据
        foreach($arr1 as $k){
            Db::name('user_position')->where('uid='.$id)->where('pid='.$k)->delete();
        }
        $this->redirect('index/user/mycollect');
    }

//    邀请页面
    public function myinvite($status = 1)
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        根据传输的status来进行邀请查询
        $data = Db::name('invitation')->field('pid,cid')->where('uid='.$id)->where('status='.$status)->select();
//        循环查询职位信息
        $position = array();
        foreach($data as $k=>$v){
            $position[] = Db::name('position')
                ->field('position_id,position_name,position_companyid,position_place,position_start,position_end')
                ->where('position_id='.$v['pid'])
                ->find();
        }
//        统计不同状态的邀请个数
        $num[] = Db::name('invitation')->where('uid='.$id)->where('status = 1')->count();
        $num[] = Db::name('invitation')->where('uid='.$id)->where('status = 2')->count();
        $num[] = Db::name('invitation')->where('uid='.$id)->where('status = 3')->count();

        $this->assign('num',$num);
        $this->assign('flag',$status);
        $this->assign('arr',$position);
        return view('user/myinvite');
    }

//    拒绝邀请
    public function inviteno($position_id)
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        字符串转换数组
        $arr1 = explode(',',$position_id);
//        需要修改的字段值
        $edit = array('status'=>3);
//        循环修改邀请状态
        foreach($arr1 as $k){
            Db::name('invitation')
                    ->where('uid='.$id)
                    ->where('pid='.$k)
                    ->where('status = 1')
                    ->update($edit);
        }
        $this->redirect('index/user/myinvite');
    }

//    同意邀请
    public function inviteyes($position_id)
    {
        $tel = input('session.tel');
        $user_tel = substr($tel,'0','11');
        $user_id = Db::name('user')->field('user_id')->where('user_tel='.$user_tel)->find();
        $id = $user_id['user_id'];
//        字符串转换为数组
        $arr1 = explode(',',$position_id);
//        需要修改的字段值
        $edit = array('status'=>2);
//        循环修改状态
        foreach($arr1 as $k){
            Db::name('invitation')
                ->where('uid='.$id)
                ->where('pid='.$k)
                ->where('status = 1')
                ->update($edit);
        }
        $this->redirect('index/user/myinvite');
    }



}